﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace  com.lib.manager
{
    /// <summary>
    /// AB Manager
    /// </summary>
   public  class ResourceManager:MonoBehaviour
    {
       public static ResourceManager Instance = null;
        void Awake()
        {
            Instance = this;
        }

       /// <summary>
       /// Asset回调
       /// </summary>
       /// <param name="o"></param>
        public delegate void ResourceManagerCallBack(object o);


        private List<string> aleardLoad = new List<string>();


       /// <summary>
       /// 查看是否已经缓存
       /// </summary>
       /// <param name="val"></param>
       /// <returns></returns>
        public bool isCache(string val)
        {
            return asset.ContainsKey(val);
             
        }

        /// <summary>
        /// 强制卸载数据
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public bool ClearAssets(string val)
        {
            ///强制卸载
            if (asset.ContainsKey(val))
            {
                asset[val].Unload(true);
                return true;
            }
            return false;
        }

       /// <summary>
       /// 清楚所有缓存
       /// </summary>
        public void ClearAll()
        {
            foreach (var k in asset)
            {
                k.Value.Unload(true);
            }
            asset.Clear();
            aleardLoad.Clear();
            Resources.UnloadUnusedAssets();
        }

        /// <summary>
        /// 加载AssetBundle
        /// </summary>
        /// <param name="path"></param>
        /// <param name="call"></param>
        /// <param name="cache"></param>
        /// <param name="thread"></param>
        public AssetBundle LoadAssetsFromAssetBundle(string path,ResourceManagerCallBack call=null, bool cache = false,bool thread = false)
        {
            lock (asset)
            {
                if (IOManager.ExitFile(path) == false)
                {
                    return null;
                }
                if (asset.ContainsKey(path))
                {
                    return asset[path];
                }
                if (aleardLoad.Contains(path))
                {
                    Logger.Log("The AssetBundle Is Be Load Please Dispose");
                    return null;
                }
                aleardLoad.Add(path);
                if (thread)
                {
                    AssetBundle assets = AssetBundle.CreateFromFile(path);
                    if (cache)
                    {
                        asset.Add(path, assets);
                    }
                    return assets;
                }
                else
                {
                    StartCoroutine(IEnumeratorLoadAssetsFromAssetBundle(path, call, cache, thread));

                }
                return null;
            }
        }

       /// <summary>
       /// 携程加载
       /// </summary>
       /// <param name="path"></param>
       /// <param name="call"></param>
       /// <param name="cache"></param>
       /// <param name="thread"></param>
       /// <returns></returns>
        private IEnumerator  IEnumeratorLoadAssetsFromAssetBundle(string path, ResourceManagerCallBack call, bool cache = false, bool thread = false)
        {
            WWW www = new WWW(path);
            yield return null;
            if (www.error == null)
            {
                call(ResourceLoadData.getData(true, www.assetBundle));
                if (cache)
                {
                    asset.Add(path, www.assetBundle);
                }
            }
            else
            {
                call(ResourceLoadData.getData(false,null));
            }

        }


        /// <summary>
        /// Assets 缓存
        /// </summary>
        private Dictionary<string, AssetBundle> asset = new Dictionary<string, AssetBundle>();

       /// <summary>
       /// 数据缓存
       /// </summary>
        private  Dictionary<string, object> data = new Dictionary<string, object>();

       /// <summary>
       /// 获取资源
       /// </summary>
       /// <param name="name"></param>
       /// <param name="t"></param>
       /// <returns></returns>
        public object getResourceData(string name,System.Type t,bool cache = false)
        {
            if (data.ContainsKey(name))
            {
                return data[name];
            }
            var obj = Resources.Load(name,t);
            if (cache)
            {
                if (data.ContainsKey(name) == false)
                {
                    data.Add(name, obj);
                }
            }
            return obj;

        }

        public void UnLoadAtlas(string name)
        {
            if (data.ContainsKey(name))
            {
                data.Remove(name);
            }
            Resources.UnloadUnusedAssets();
        }
   
        public void Clear()
        {
            data.Clear();
            Resources.UnloadUnusedAssets();
        }

    }
}

public class ResourceLoadData
{
    public bool success { get; set; }

    public object data { get; set; }

    public static ResourceLoadData getData(bool _succ, object _data)
    {
        ResourceLoadData r = new ResourceLoadData();
        r.success = _succ;
        r.data = _data;
        return r;
    }
}